﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MenuService
{
    internal class MenuItem
    {
        private List<MenuItem> _childItems;

         public MenuItem FindItemByKey(String itemKey)
         {
             if (this.ItemKey == itemKey)
                 return this;

             foreach (MenuItem child in this.ChildItems)
             {
                 MenuItem subRes = child.FindItemByKey(itemKey);

                 if (subRes != null)
                     return subRes;
             }

             return null;
         }


        public MenuItem(String itemKey, String itemText)
        {
            this.ItemKey  = itemKey;
            this.ItemText = itemText;
            _childItems   = new List<MenuItem>();
        }

        public MenuItem(String itemKey,String itemText, MenuItem parent)
            : this(itemKey,itemText)
        {
            this.ParentItem = parent;
        }
            

        public void AddChild(String childText,String childKey)
        {
            MenuItem newItem = new MenuItem(childKey,childText,this);

            _childItems.Add(newItem);
        }

        public MenuItem              ParentItem { get; protected set; }
        public String                ItemText   { get; protected set; }
        public String                ItemKey    { get; protected set; }

        public IEnumerable<MenuItem> ChildItems 
        {
            get { return _childItems; }
        }
       
    }
}
